package com.zyh.shortlick.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyh.shortlick.admin.common.biz.user.UserContext;
import com.zyh.shortlick.admin.common.convention.exception.ServiceException;
import com.zyh.shortlick.admin.common.convention.result.Result;
import com.zyh.shortlick.admin.dao.entity.GroupDO;
import com.zyh.shortlick.admin.dao.mapper.GroupMapper;
import com.zyh.shortlick.admin.remote.dto.ShortLinkActualRemoteService;
import com.zyh.shortlick.admin.remote.dto.req.ShortLinkRecycleBinPageReqDTO;
import com.zyh.shortlick.admin.remote.dto.resp.ShortLinkPageRespDTO;
import com.zyh.shortlick.admin.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "recycleBinServiceImplByAdmin")
@RequiredArgsConstructor
public class RecycleBinServiceImpl implements RecycleBinService {

    private final ShortLinkActualRemoteService shortLinkActualRemoteService;
    private final GroupMapper groupMapper;

//    分页重构就是查到到用户的gid然后再调用远程服务的接口
    @Override
    public Result<Page<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<GroupDO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GroupDO::getUsername, UserContext.getUsername())
                .eq(GroupDO::getDelFlag,0);
        List<GroupDO> groupDOS = groupMapper.selectList(lqw);
        if (groupDOS.isEmpty()){
            throw new ServiceException("用户无分组信息");
        }
        requestParam.setGidList(groupDOS.stream().map(GroupDO::getGid).toList());
        return shortLinkActualRemoteService.pageRecycleBinShortLink(requestParam.getGidList(),requestParam.getCurrent(),requestParam.getSize());
    }
}
